package Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import DAO.BoxDao;
import DAO.ParcelDao;
import DAO.UserDao;
import POJO.Parcel;
import POJO.Terminal;
import POJO.User;
import Thread.AnJieRequestThread;
import Utils.TimeHelper;

@Controller
@RequestMapping(value="user")
public class UserController {
//	static Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	private ParcelDao parcelDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private BoxDao boxDao;
	
	@RequestMapping("parcels")
	@ResponseBody
	public Map<String,Object> userParcels(HttpSession httpSession){
		HashMap<String,Object> response=new HashMap<String,Object>();
		User user=(User)httpSession.getAttribute("user");
		Terminal terminal=(Terminal)httpSession.getAttribute("terminal");
		ArrayList<Parcel> parcels=parcelDao.returnParcelInfoByPhoneNum(terminal, user);
		if(parcels!=null){
			for(Parcel p:parcels){
				if(boxDao.queryBoxStatus(terminal, Integer.parseInt(p.getBoxSeq()))==1){
					p.setBoxStatus(0);
				}else{
					p.setBoxStatus(1);
				}
			}
			response.put("result", "0");
			response.put("phoneNum", user.getMobileNum());
			response.put("parcelInfo", parcels);
		}else{
			response.put("result", "1");
		}
		return response;

	}
	
	
	/**
	 * 用户取走包裹
	 * @param httpSession
	 * @param parcelNum
	 * @param doorStatus
	 * @param goodsStatus
	 * @param boxSeq
	 * @return
	 */
	@RequestMapping("getParcel")
	@ResponseBody
	public synchronized Map<String,Object> getParcel(HttpSession httpSession,
							@RequestParam("parcelNum")String parcelNum,
							@RequestParam(value = "doorStatus", defaultValue = "-1") String doorStatus,
							@RequestParam(value = "goodsStatus", defaultValue = "-1") String goodsStatus,
							@RequestParam(value = "costTotal", defaultValue = "-1") String costTotal,
							@RequestParam(value = "costWay", defaultValue = "-1") String costWay,
							@RequestParam("boxSeq")String boxSeq){
		HashMap<String,Object> response=new HashMap<String,Object>();
		User user=(User)httpSession.getAttribute("user");
		Terminal terminal=(Terminal)httpSession.getAttribute("terminal");
		if(boxDao.queryBoxStatus(terminal, Integer.parseInt(boxSeq))==1){
			if(parcelDao.updateParcelStatement(parcelNum, user, terminal,boxSeq)&&boxDao.boxToggleAvailable(terminal, boxSeq, 0)&&boxDao.setBoxParcel(terminal.getTerminalID(), Integer.parseInt(boxSeq), 0)){
				Parcel parcel = parcelDao.queryParcel(terminal.getTerminalID(), parcelNum, user.getMobileNum(), 1);
				if((terminal.getUpload()==1&&(parcel.getBelong().contains("韵达")||parcel.getBelong().contains("汇通")))||terminal.getAgentID()==29||terminal.getAgentParentID()==29){
					AnJieRequestThread rnt = new AnJieRequestThread();
					rnt.setUrl("http://42.121.192.78:8086/ourangel-box-rpc/terminalSystem/updatePackage.html");
					rnt.setSign("c8db7d0e24e9a4ac3e42ec9c06ddc5f7");
					rnt.setParcelId(String.valueOf(parcel.getId()));
					rnt.setParcelNum(parcel.getParcelNum());
					rnt.setParcelStatus(parcel.getStatus());
					rnt.setGetTime(parcel.getGetTime().toString());
					Thread thread = new Thread(rnt);
					thread.start();
				}
				if("1".equals(doorStatus)&&"1".equals(goodsStatus)){
					int logID = parcelDao.saveAbnormalParcelLog(parcel, doorStatus, goodsStatus);
					boxDao.saveAbnormalBoxLog(parcel, logID, "门开启， 无物状态。", "物品模块故障或物品太小未检测到亦或采用非常规方式已经拿出包裹，或未存入包裹。");
				}
				if(!"-1".equals(costTotal)&&!"-1".equals(costWay)){
					parcelDao.saveUserCostLog(parcel, terminal, costTotal, costWay);
				}
				response.put("result", "0");
			}else{
//				logger.info("[Error]user------------>getParcel出错");
				response.put("result", "1");
			}
		}else{
			response.put("result", "2");
		}
		if(!TimeHelper.isNowLateSeconds(parcelDao.queryParcel(terminal.getTerminalID(), parcelNum, user.getMobileNum(),1).getGetTime(), 20)){
			response.put("result", "0");
		}
		return response;
		
	}


	
	@RequestMapping("quit")
	@ResponseBody	
	public Map<String,String> quit(HttpSession httpSession,
				HttpServletRequest request){
		HashMap<String,String> response=new HashMap<String,String>();
		Terminal terminal=(Terminal)httpSession.getAttribute("terminal");
		User user=(User)httpSession.getAttribute("user");
		if(parcelDao.isExistParcelByCheckCode(terminal,user.getCheckCode())==null){
//			logger.info("remove checkout cache");
			request.getServletContext().removeAttribute(terminal.getTerminalID()+user.getCheckCode());
		}
		httpSession.removeAttribute("user");
		response.put("result", "0");
		return response;
	}
	
}
